package com.yixia.system.mapper.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yixia.common.utils.StringUtils;
import com.yixia.system.domain.system.SysUserRole;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 35038
* @description 针对表【sys_user_role】的数据库操作Mapper
* @createDate 2023-06-02 19:15:18
* @Entity com.java1234.entity.SysUserRole
*/
@Mapper
public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {

    /**
     * 根据角色id统计使用该ID的用户的个数
     * @param roleId
     * @return
     */
    default int countUserRoleByRoleId(Long roleId){
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getRoleId, roleId);
        Integer count = Math.toIntExact(this.selectCount(queryWrapper));
        return count;
    }

    /**
     * 删除授权用户
     * @param userRole
     * @return
     */
    default boolean deleteUserRoleInfo(SysUserRole userRole){
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getRoleId, userRole.getRoleId())
                .eq(SysUserRole::getUserId, userRole.getUserId());
        int delete = delete(queryWrapper);
        return  delete > 0;
    }


    /**
     * 批量删除授权用户
     * @param roleId
     * @param userIds
     * @return
     */
    default boolean deleteUserRoleInfos(Long roleId, Long[] userIds){
        List<Long> userIdList = Arrays.asList(userIds);
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getRoleId, roleId)
                .in(SysUserRole::getUserId, userIdList);
        int delete = delete(queryWrapper);
        return delete > 0;
    }

    /**
     * 根据用户的ID进行删除所有角色权限
     * @param userId
     */
    default void deleteUserRoleByUserId(Long userId){
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, userId);
        this.delete(queryWrapper);
    }

    /**
     * 批量授予用户角色权限
     * @param userId
     * @param roleIds
     */
    default void insertUserRole(Long userId, Long[] roleIds){
        if (StringUtils.isNotNull(roleIds)) {
            for (Long roleId : roleIds) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setRoleId(roleId);
                sysUserRole.setUserId(userId);
                /*添加*/
                this.insert(sysUserRole);
            }
        }
    }

    /**
     * 根据ID获取SysUserRole
     * @param userId
     * @return
     */
    default List<SysUserRole> selectListByUserId(Long userId){
        /*1. 先根据用户ID查询用户角色表*/
        LambdaQueryWrapper<SysUserRole> userRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRoleLambdaQueryWrapper.eq(SysUserRole::getUserId, userId);
        return this.selectList(userRoleLambdaQueryWrapper);
    }

    /**
     * 更新用户角色表
     * @param userId 用户ID
     * @param roleIds 角色ID
     */
    default void updateUserRole(Long userId, Long[] roleIds){
        /*1. 先根据userId删除之前的记录*/
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, userId);
        this.delete(queryWrapper);
        /*2. 将新的数据添加进去*/
        insertUserRole(userId, roleIds);
    }

    /**
     * 批量删除用户角色表信息---通过userId
     * @param userIds 用户ID
     */
    default void deleteUserRoleByUserIds(Long[] userIds){
        if(userIds.length > 0){
            List<Long> collect = Arrays.stream(userIds).collect(Collectors.toList());
            LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(SysUserRole::getUserId, collect);
            this.delete(queryWrapper);
        }
    }
}




